package com.iiot.manage.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.iiot.manage.entity.*;
import com.iiot.manage.exception.ResponseBean;
import com.iiot.manage.exception.ResponseErrorCodeEnums;
import com.iiot.manage.service.*;
import com.iiot.manage.shiro.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/iiotApi")
public class IIotApiController  {


    @CrossOrigin
    @RequestMapping(value = "/token",method = RequestMethod.GET)
    public ResponseBean<String> token(@RequestParam("username") String username,
                                                         @RequestParam("password") String password){
            return new ResponseBean(true,JWTUtil.sign(username, password), ResponseErrorCodeEnums.SUCCESS_OPTION);
    }

    @Autowired
    ProjectInfoService projectInfoService;

    @CrossOrigin
    @RequestMapping(value = "/project/{code}",method = RequestMethod.GET)
    public ResponseBean<ProjectInfo> GetProjectInfo(@PathVariable Long code){
        EntityWrapper<ProjectInfo> entityWrapper=  new EntityWrapper<ProjectInfo>();
        entityWrapper.where("code="+code);
         ProjectInfo projectInfo= projectInfoService.selectOne(entityWrapper);
        return new ResponseBean(true,projectInfo, ResponseErrorCodeEnums.SUCCESS_OPTION);
    }

   @Autowired
    DeviceVwService deviceVwService;

    @CrossOrigin
    @RequestMapping(value = "/{projectCode}/devices",method = RequestMethod.GET)
    public ResponseBean<List<DeviceVw>> GetDevicesByProjectCode(@PathVariable Long projectCode){

        EntityWrapper<DeviceVw> entityWrapper=  new EntityWrapper<DeviceVw>();
        entityWrapper.where("projectCode="+projectCode);
        List<DeviceVw> deviceVws= deviceVwService.selectList(entityWrapper);
        return new ResponseBean(true,deviceVws, ResponseErrorCodeEnums.SUCCESS_OPTION);
    }

    @Autowired
    DeviceTagService deviceTagService;

    @Autowired
    DeviceParameterService deviceParameterService;


    @Autowired
    DeviceIndexService deviceIndexService;

    @CrossOrigin
    @RequestMapping(value = "/{deviceCode}/device_tags",method = RequestMethod.GET)
    public ResponseBean<List<DeviceTag>> GetDeviceTagByDeviceCode(@PathVariable Long deviceCode){

        EntityWrapper<DeviceTag> entityWrapper=  new EntityWrapper<>();
        entityWrapper.where("deviceCode="+deviceCode);
        List<DeviceTag> deviceVws= deviceTagService.selectList(entityWrapper);
        return new ResponseBean(true,deviceVws, ResponseErrorCodeEnums.SUCCESS_OPTION);
    }


    @CrossOrigin
    @RequestMapping(value = "/{deviceCode}/device_parameters",method = RequestMethod.GET)
    public ResponseBean<List<DeviceParameter>> GetDeviceParametersByDeviceCode(@PathVariable Long deviceCode){

        EntityWrapper<DeviceParameter> entityWrapper=  new EntityWrapper<>();
        entityWrapper.where("deviceCode="+deviceCode);
        List<DeviceParameter> deviceVws= deviceParameterService.selectList(entityWrapper);
        return new ResponseBean(true,deviceVws, ResponseErrorCodeEnums.SUCCESS_OPTION);
    }


    @CrossOrigin
    @RequestMapping(value = "/{deviceCode}/device_indexes",method = RequestMethod.GET)
    public ResponseBean<List<DeviceIndex>> GetDeviceIndexesByDeviceCode(@PathVariable Long deviceCode){

        EntityWrapper<DeviceIndex> entityWrapper=  new EntityWrapper<>();
        entityWrapper.where("deviceCode="+deviceCode);
        List<DeviceIndex> deviceVws= deviceIndexService.selectList(entityWrapper);
        return new ResponseBean(true,deviceVws, ResponseErrorCodeEnums.SUCCESS_OPTION);
    }
}
